//
//  ViewFirstSection.m
//  TasteEnjoy
//
//  Created by XuRui on 16/5/1.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#import "ViewFirstSection.h"

@interface ViewFirstSection ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, retain) BaseCollectionView *collectionView;  /**< collection */
@property (nonatomic, retain) UICollectionViewFlowLayout *findLayout;

@end

@implementation ViewFirstSection

#pragma mark - ------------ 内存管理 ---------------
- (void)dealloc
{
    _collectionView.delegate = nil;
    _collectionView.dataSource = nil;
    [_collectionView release];
    [_findLayout release];
    [_arrPop release];
    Block_release(_BlockIndexPath);
    [super dealloc];
    
}

#pragma mark - ------------ 初始化 ---------------
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creteCollection];
    }
    return self;
}

#pragma mark - ------------ layout ---------------
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _collectionView.frame = self.bounds;
    
}
#pragma mark - ------------ 创建collectionView ---------------
- (void)creteCollection
{

    
    self.findLayout = [[UICollectionViewFlowLayout alloc]init];
   _findLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
   _findLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[BaseCollectionView alloc]initWithFrame:self.bounds collectionViewLayout:_findLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self addSubview:_collectionView];
    
    //注册头部cell 和cell
    [_collectionView registerClass:[FindTopCollectionViewCell class] forCellWithReuseIdentifier:@"find"];

    
    [_findLayout release];
    [_collectionView release];
    
}


#pragma mark - ------------ cell赋值 根据不同分区 传不同数组 ---------------
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    FindTopCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"find" forIndexPath:indexPath];
    
    FindModel *m = _arrPop[indexPath.item];
    
    cell.model = m;
    return cell;
    
}
    

#pragma mark - ------------ 按分区返回item个数 ---------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
        
    return _arrPop.count;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    self.BlockIndexPath (indexPath);
   // NSLog(@"%ld", indexPath.item);

}


- (void)clickFirstSectionView:(void (^)(NSIndexPath *))BlockIndexPath
{

    self.BlockIndexPath = BlockIndexPath;

}

#pragma mark - ------------ 返回item大小 ---------------

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
   
    return CGSizeMake(100, 120);
    
}


- (void)setArrPop:(NSArray *)arrPop
{

    if (_arrPop != arrPop) {
        [_arrPop release];
        _arrPop = [arrPop retain];
    }
    [_collectionView reloadData];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
